/**
 * @(#)com.ztesoft.res.frame.web.util.IPUtils.java
 * <br/>
 *<b>Copyright (c)</b> 2017
 * @Date 2017年8月2日
 */
package com.ztesoft.huangfu.learning.springboot.springbootcommon.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Descrip:获取IP地址相关工具类
 *
 * @author
 * @version 1.0
 * @since 2017年8月2日 下午1:01:14
 */
public abstract class IPUtils {
	private static final Logger logger = LoggerFactory.getLogger(IPUtils.class);

	/**
	 * 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址;
	 *
	 * @param request
	 * @return
	 * @throws
	 */
	public static final String getIpAddress(HttpServletRequest request) {
		// 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址

		String ip = request.getHeader("X-Forwarded-For");
		if (logger.isDebugEnabled()) {
			logger.debug("getIpAddress(HttpServletRequest) - X-Forwarded-For - String ip={}", ip);
		}

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("Proxy-Client-IP");
				if (logger.isDebugEnabled()) {
					logger.debug("getIpAddress(HttpServletRequest) - Proxy-Client-IP - String ip={}", ip);
				}
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("WL-Proxy-Client-IP");
				if (logger.isDebugEnabled()) {
					logger.debug("getIpAddress(HttpServletRequest) - WL-Proxy-Client-IP - String ip={}", ip);
				}
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("HTTP_CLIENT_IP");
				if (logger.isDebugEnabled()) {
					logger.debug("getIpAddress(HttpServletRequest) - HTTP_CLIENT_IP - String ip={}", ip);
				}
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("HTTP_X_FORWARDED_FOR");
				if (logger.isDebugEnabled()) {
					logger.debug("getIpAddress(HttpServletRequest) - HTTP_X_FORWARDED_FOR - String ip={}", ip);
				}
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();
				if (logger.isDebugEnabled()) {
					logger.debug("getIpAddress(HttpServletRequest) - getRemoteAddr - String ip={}", ip);
				}
			}
		} else if (ip.length() > 15) {
			String[] ips = ip.split(",");
			for (int index = 0; index < ips.length; index++) {
				String strIp = (String) ips[index];
				if (!("unknown".equalsIgnoreCase(strIp))) {
					ip = strIp;
					break;
				}
			}
		}
		return ip;
	}

	private static final String REFER_FIELD = "Referer";

	/**
	 * 获取Referer来源页的IP地址
	 * @param request
	 * @return
	 */
	public static final String getReferIP(HttpServletRequest request){
		String url = request.getHeader(REFER_FIELD);
		if(!StringUtils.hasText(url)){
			url = request.getHeader(REFER_FIELD.toLowerCase());
		}
		return url;
	}


	/**
	 * 获取请求的IP地址和端口
	 * @param request
	 * @param hasPort 是否包含端口
	 * @return
	 */
	public static final String getRequestAddr(HttpServletRequest request, boolean hasPort){
		String url = request.getRequestURL().toString();
		return getRequestAddr(url, hasPort);
	}



	private static final Pattern IP_PORT_RULE = Pattern.compile("(\\d+\\.\\d+\\.\\d+\\.\\d+:\\d+)");
	private static final Pattern IP_RULE = Pattern.compile("(\\d+\\.\\d+\\.\\d+\\.\\d+)");



	/**
	 * 获取url地址中的IP地址和端口
	 * @param url
	 * @param hasPort 是否包含端口
	 * @return
	 */
	public static final String getRequestAddr(String url, boolean hasPort){
		Matcher m = null;
		if(hasPort){
			m = IP_PORT_RULE.matcher(url);
		}else{
			m = IP_RULE.matcher(url);
		}
		while(m.find()) {
			  return m.group(1);
		}
		if(hasPort){
			return getRequestAddr(url, false);
		}
		return null;
	}


	public static void main(String[] args) {
		String s = "11";
		int i = Integer.valueOf(s);
		System.out.println(String.valueOf(i+1));
	}
}
